package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.databinding.Bindable;
import android.view.View;
import android.widget.Toast;

//import org.greenrobot.greendao.annotation.Id;
//import org.greenrobot.greendao.annotation.Property;

import org.greenrobot.greendao.annotation.Entity;
import org.greenrobot.greendao.annotation.Id;
import org.greenrobot.greendao.annotation.Keep;
import org.greenrobot.greendao.annotation.Property;
import org.greenrobot.greendao.annotation.Transient;

import java.util.Timer;
import java.util.TimerTask;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.DBManager;
import chuangyi.com.org.DOMIHome.HomeActivity;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.LoginActivity;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;

import org.greenrobot.greendao.annotation.Generated;
import org.greenrobot.greendao.annotation.Unique;

/**
 * Created by zxh on 16/10/29.
 */
@Entity
public class User extends ContextObservable {
    @Property(nameInDb = "phone")
    private String phone;
    @Transient
    private String password;
    @Unique
    @Property(nameInDb = "uid")
    private int uid;
    @Property(nameInDb = "token")
    private String token;
    @Transient
    private String sendMessage;
    @Transient
    private boolean allowSend;
    @Transient
    static User mCurrent;

    private static final Pattern phoneMatcher = Pattern.compile("[0-9]{11}");

    public static User getCurrentUser() {
        return mCurrent;
    }

    public static void setCurrentUser(User user) {
        mCurrent = user;
    }

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    @Bindable
    public String getSendMessage() {
        return sendMessage;
    }

    public void setSendMessage(String sendMessage) {
        this.sendMessage = sendMessage;
        notifyPropertyChanged(BR.sendMessage);
    }

    @Bindable
    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
        notifyPropertyChanged(BR.phone);
    }

    @Bindable
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
        notifyPropertyChanged(BR.password);
    }

    public User(Context context) {
        super(context);
        sendMessage = "获取验证码";
        allowSend = true;
    }

    @Keep
    @Generated(hash = 460575629)
    public User(String phone, int uid, String token) {
        super(null);
        this.phone = phone;
        this.uid = uid;
        this.token = token;
    }

    @Keep
    @Generated(hash = 586692638)
    public User() {
        super(null);
    }

    /**
     * 用户登录
     *
     * @param view
     */
    public void LoginClick(View view) {
        if (Utils.isNullOrEmpty(getPassword())) {
            Toast.makeText((Activity) super.getContext(), "请输入验证码", Toast.LENGTH_SHORT).show();
            return;
        }
        final Context context = super.getContext();
        if (validPhone(phone, context)) {
            final LoginActivity activity = (LoginActivity) context;
            if (activity.isDestroyed())
                return;
            LoadingDialog.getInstance(context).show();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final User user = WebApi.login(context, phone, password);
                    activity.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            LoadingDialog.getInstance(context).dismiss();
//                            判断用户是否登录成功
                            if (user != null && "".equals(user.getToken()) == false && user.getToken() != null) {
                                user.setPhone(phone);
                                Toast.makeText(context, "登录成功", Toast.LENGTH_LONG).show();
                                Intent intent = new Intent();
                                intent.setClass(activity, HomeActivity.class);
                                activity.startActivity(intent);
//                                user.setUid(1);
                                User.setCurrentUser(user);
                                DBManager.getInstance(context.getApplicationContext()).deleteAllUser();
                                DBManager.getInstance(context.getApplicationContext()).insertUser(user);
                            }
                        }
                    });
                }
            }).start();
        }
    }

    public void SendSMSClick(View view) {
        if (allowSend && validPhone(phone, view.getContext())) {
            final Context context = super.getContext();
            final Timer timer = new Timer();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (WebApi.SendSMS(context, phone)) {
                        allowSend = false;
                        TimerTask task = new TimerTask() {
                            int count = 60;

                            @Override
                            public void run() {
                                if (count > 0) {
                                    setSendMessage(String.valueOf(count) + "秒后重发");
                                } else {
                                    allowSend = true;
                                    timer.cancel();
                                    timer.purge();
                                    setSendMessage("重新获取");
                                }
                                count--;
                            }
                        };
                        timer.schedule(task, 0, 1000);
                    }
                }
            }).start();
        }
    }

    private boolean validPhone(String phone, Context context) {
        if (null == phone || "".equals(phone)) {
            Toast.makeText(context, "手机号不能为空", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            Matcher matcher = phoneMatcher.matcher(phone);
            if (matcher.matches() == false) {
                Toast.makeText(context, "手机号码格式不正确", Toast.LENGTH_SHORT).show();
                return false;
            }
        }
        return true;
    }
}
